Initialization
import pandas as pd;
import numpy as np;
import plotly.express as px
artists=pd.read_csv("data/artists.csv")
tracks = pd.read_csv("data/tracks.csv")
tracks["id_artists"] = tracks["id_artists"].apply(lambda x: x.strip('][').split(',')) #convert arrays
tracks["id_artists"]=tracks["id_artists"].apply(lambda x:x[0].replace("'",""))
combinedDf=tracks.merge(artists,left_on="id_artists",right_on="id")
combinedDf=combinedDf.rename(columns={"name_x":'title',"name_y":"artist_name", "popularity_x":"song_popularity","popularity_y":"artist_popularity","id_x":"song_id","id_y":"artist_id","macro genre":"artist_genre"})
combinedDf["tempo"]=combinedDf["tempo"].round(0)
combinedDf.loc[combinedDf["artist_name"]=="Red Hot Chili Peppers"]
| song_id | title | song_popularity | duration_ms | explicit | artists | id_artists | release_date | danceability | energy | ... | valence | tempo | time_signature | artist_id | followers | genres | artist_name | artist_popularity | TmpGenres | artist_genre | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 218042 | 0ytvC9tG03obop9RXqG3af | Out In L.A. - 2002 Digital Remaster | 53 | 120667 | 1 | ['Red Hot Chili Peppers'] | 0L8ExT028jH3ddEcZwqJJ5 | 1984-08-10 | 0.772 | 0.888 | ... | 0.706 | 125.0 | 4 | 0L8ExT028jH3ddEcZwqJJ5 | 15797256.0 | ['alternative rock', 'funk metal', 'funk rock'... | Red Hot Chili Peppers | 85 | ["'alternative rock'", " 'funk metal'", " 'fun... | rock |
| 218043 | 0fZu9ojDt6XZLDg6dos2EM | If You Want Me To Stay - Remastered | 40 | 247333 | 0 | ['Red Hot Chili Peppers'] | 0L8ExT028jH3ddEcZwqJJ5 | 1985-08-16 | 0.829 | 0.791 | ... | 0.794 | 100.0 | 4 | 0L8ExT028jH3ddEcZwqJJ5 | 15797256.0 | ['alternative rock', 'funk metal', 'funk rock'... | Red Hot Chili Peppers | 85 | ["'alternative rock'", " 'funk metal'", " 'fun... | rock |
| 218044 | 3eiIQPGp6biDu91bKgAEzg | Me And My Friends | 53 | 189133 | 0 | ['Red Hot Chili Peppers'] | 0L8ExT028jH3ddEcZwqJJ5 | 1987 | 0.580 | 0.996 | ... | 0.202 | 100.0 | 4 | 0L8ExT028jH3ddEcZwqJJ5 | 15797256.0 | ['alternative rock', 'funk metal', 'funk rock'... | Red Hot Chili Peppers | 85 | ["'alternative rock'", " 'funk metal'", " 'fun... | rock |
| 218045 | 6NeRpRTcFftvt3j1vF4Pcw | Fight Like A Brave | 43 | 233760 | 0 | ['Red Hot Chili Peppers'] | 0L8ExT028jH3ddEcZwqJJ5 | 1987 | 0.719 | 0.936 | ... | 0.276 | 104.0 | 4 | 0L8ExT028jH3ddEcZwqJJ5 | 15797256.0 | ['alternative rock', 'funk metal', 'funk rock'... | Red Hot Chili Peppers | 85 | ["'alternative rock'", " 'funk metal'", " 'fun... | rock |
| 218046 | 5arVt2Wg0zbiWwAOZef2Nl | Higher Ground - Remastered | 67 | 202053 | 0 | ['Red Hot Chili Peppers'] | 0L8ExT028jH3ddEcZwqJJ5 | 1989-08-16 | 0.476 | 0.979 | ... | 0.416 | 140.0 | 4 | 0L8ExT028jH3ddEcZwqJJ5 | 15797256.0 | ['alternative rock', 'funk metal', 'funk rock'... | Red Hot Chili Peppers | 85 | ["'alternative rock'", " 'funk metal'", " 'fun... | rock |
| ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
| 218206 | 2h3DA5sidcqjywLh36RFkU | Coffee Shop | 41 | 188933 | 0 | ['Red Hot Chili Peppers'] | 0L8ExT028jH3ddEcZwqJJ5 | 1995-09-12 | 0.496 | 0.978 | ... | 0.589 | 121.0 | 4 | 0L8ExT028jH3ddEcZwqJJ5 | 15797256.0 | ['alternative rock', 'funk metal', 'funk rock'... | Red Hot Chili Peppers | 85 | ["'alternative rock'", " 'funk metal'", " 'fun... | rock |
| 218207 | 6jR4FKWiil1FrsfEMt7jqu | Pea | 39 | 107307 | 1 | ['Red Hot Chili Peppers'] | 0L8ExT028jH3ddEcZwqJJ5 | 1995-09-12 | 0.609 | 0.111 | ... | 0.298 | 177.0 | 4 | 0L8ExT028jH3ddEcZwqJJ5 | 15797256.0 | ['alternative rock', 'funk metal', 'funk rock'... | Red Hot Chili Peppers | 85 | ["'alternative rock'", " 'funk metal'", " 'fun... | rock |
| 218208 | 4VgylHaBo55VXqX4AiZiyc | Deep Kick | 38 | 393907 | 1 | ['Red Hot Chili Peppers'] | 0L8ExT028jH3ddEcZwqJJ5 | 1995-09-12 | 0.464 | 0.918 | ... | 0.322 | 117.0 | 4 | 0L8ExT028jH3ddEcZwqJJ5 | 15797256.0 | ['alternative rock', 'funk metal', 'funk rock'... | Red Hot Chili Peppers | 85 | ["'alternative rock'", " 'funk metal'", " 'fun... | rock |
| 218209 | 3cWEHEBIJzikXa9xPzMQkC | Backwoods | 35 | 188227 | 0 | ['Red Hot Chili Peppers'] | 0L8ExT028jH3ddEcZwqJJ5 | 1987 | 0.578 | 0.957 | ... | 0.468 | 111.0 | 4 | 0L8ExT028jH3ddEcZwqJJ5 | 15797256.0 | ['alternative rock', 'funk metal', 'funk rock'... | Red Hot Chili Peppers | 85 | ["'alternative rock'", " 'funk metal'", " 'fun... | rock |
| 218210 | 6ynGyjRka7Jin0xEw4Zm0k | Fortune Faded | 24 | 201267 | 0 | ['Red Hot Chili Peppers'] | 0L8ExT028jH3ddEcZwqJJ5 | 2003-11-18 | 0.524 | 0.946 | ... | 0.613 | 119.0 | 4 | 0L8ExT028jH3ddEcZwqJJ5 | 15797256.0 | ['alternative rock', 'funk metal', 'funk rock'... | Red Hot Chili Peppers | 85 | ["'alternative rock'", " 'funk metal'", " 'fun... | rock |
169 rows × 27 columns
df=combinedDf.groupby(["artist_genre"],as_index=False)["energy"].median()
fig=px.histogram(df,x="artist_genre",y="energy")
fig.update_xaxes(categoryorder="total descending")
fig.layout["xaxis"]["title"]="Generes"
fig.layout["yaxis"]["title"]="Median of energy"
fig.layout.yaxis.dtick=0.5
fig.show()
df=combinedDf.groupby(["artist_genre"],as_index=False)["valence"].mean()
fig=px.histogram(df,x="artist_genre",y="valence")
fig.update_xaxes(categoryorder="total descending")
fig.layout["xaxis"]["title"]="Generes"
fig.layout["yaxis"]["title"]="Valence"
fig.layout.yaxis.dtick=0.5
fig.show()
combinedDf["release_year"]=combinedDf["release_date"].apply(lambda e:e.split("-")[0])
tmp=combinedDf.loc[(combinedDf["artist_genre"] == "metal") & (combinedDf["release_year"] >= "1971") ]
tmp["release_year"].dropna()
df=tmp.set_index(pd.to_datetime(tmp['release_year'])).resample("2Y")["valence"].mean()
tmp=combinedDf.loc[(combinedDf["artist_genre"] == "pop") & (combinedDf["release_year"] >= "1971")]
tmp["release_year"].dropna()
df2=tmp.set_index(pd.to_datetime(tmp['release_year'])).resample("2Y")["valence"].mean()
dataframe=pd.DataFrame({"valence":df})
dataframe2=pd.DataFrame({"valence":df2})
dataframe["artist_genre"]="metal"
dataframe2["artist_genre"]="pop"
dataframe=dataframe.append(dataframe2)
/tmp/ipykernel_198044/1592222919.py:17: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.
fig=px.line(dataframe,y="valence",color='artist_genre')
fig.layout["xaxis"]["title"]="Year"
fig.layout["yaxis"]["title"]="Valence"
fig.layout["legend"]["title"]="Genre"
fig.add_annotation(dict(font=dict(color='white',size=15),
x=0,
y=-0.1,
showarrow=False,
text="Sand, dark, negative",
textangle=0,
xanchor='left',
xref="paper",
yref="paper"))
fig.add_annotation(dict(font=dict(color='white',size=15),
x=0,
y=1.1,
showarrow=False,
text="Happy, positive",
textangle=0,
xanchor='left',
xref="paper",
yref="paper"))
fig.show()
combinedDf["tempoType"]="Lento (0-90) bpm"
combinedDf.loc[(combinedDf["tempo"]>=90),"tempoType"]="Normal (90-130) bpm"
combinedDf.loc[(combinedDf["tempo"]>130),"tempoType"]="Speed (130-360) bpm"
df=combinedDf.groupby("tempoType",as_index=False)["valence"].mean()
fig=px.histogram(df,x="tempoType",y="valence" ,histfunc="avg", category_orders={"Lento":"0-90",
"Normal": "90-130",
"Speed": "130-360"})
fig.layout["xaxis"]["title"]="Type of Tempo"
fig.layout["yaxis"]["title"]="Valence"
fig.show()